<?php
	$keyword = trim($_POST['keyword']);
	$bytype = $_POST['bytype'];
	$page = $_POST['page'];
	if($page==null || $page=='' || $page < 0){
		$page='0';
	}
	if($keyword==null || $keyword==''){
		$search_result_display = 'none';
	}else{
		$search_result_display = 'block';
	}
?>

<html>

<head>
	
	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript">
		function previousPage(){
			var current = parseInt(document.paging.page.value);
			current = current-1;
			document.paging.page.value = current;
			document.paging.submit();
		}
		function nextPage(){
			var current = parseInt(document.paging.page.value);
			current = current+1;
			document.paging.page.value = current;
			document.paging.submit();
		}
	</script>
	
	<style type="text/css"> 
		#search_input{
			margin:5px;
			padding:5px;
			background:#e5eecc;
			border:solid 2px #c3c3c3;
		}
                
       #search_result{
            margin:5px;
			padding:5px;
			background:#F5F5DC;
			border:solid 2px #c3c3c3;
        }	
	   table,td,th{
			margin:5px;
			padding:5px;
			border:solid 2px #191970; 
			border-collapse:collapse;
            background-color:#F8F8FF;
	   }
	   table{
			width:99%;
	   }
	</style>

</head>
 
<body>
	
	<p>
		Enter your keyword, either a CVE name or a product name, into the textbox to search for CVEs.
	</p>

	<div id="search_input">
		<fieldset>
			  <legend>Keyword</legend>
			  <form method="post" action="">
					<input type="radio" name="bytype" value="cvename" checked="checked"/> Search by CVE name
					<input type="radio" name="bytype" value="product" /> Search by product name
					<br/>
					<input type="text" name="keyword"/>
					<input type="submit" name="submit" value="Search!"/>
			  </form>
		</fieldset>
	</div>

	<div id="search_result" style="display:<?php echo $search_result_display?>">
		<?php
			if($keyword!=null && $keyword!=''){
                global $global_ws_address;

				$context = stream_context_create(array(
					'http' => array(
						'timeout' => 10      // Timeout in seconds
					)
				));

				if($bytype=='cvename'){
					$url = $global_ws_address.'o=searchn&p1='.$keyword.'&p2='.$page;
				}elseif($bytype=='product'){
					$url = $global_ws_address.'o=searchp&p1='.$keyword.'&p2='.$page;
				}
					$result = @file_get_contents($url,0,$context);
					$result = json_decode($result, true);
				if( !is_array($result)){
					echo 'Keyword: "'.$keyword.'" - no result found.';
				}else{
					echo 'Keyword: "'.$keyword.'" (Sort by Relevancy Score)'.' PAGE:'.($page+1);
		?>
					<table>
						<tr>
							<th>CVE name</th>
							<th>Description</th>
						</tr>
		<?php
					foreach($result as $value){
						echo '<tr>';
						echo '<td style="width:30%">';
							echo '<a href="?q=node/14&cve='.$value['cvename'].'">'.$value['cvename'].'</a>';
						echo '</td>';
						echo '<td style="width:70%">';
							echo '<p style="text-align:justify">'.$value['description'].'</p>';
						echo '</td>';
						echo "</tr>";
					}
		?>
					</table>
		<?php
				}
		?>
					<form name="paging" style="display:none" method="post" action="">
						<input type="hidden" name="keyword" value="<?php echo $keyword?>">
						<input type="hidden" name="page" value="<?php echo $page?>">
						<input type="hidden" name="bytype" value="<?php echo $bytype?>">
					</form>
					<div style="text-align:center">
						<a href="javascript:previousPage()"> Previous Results </a>
						<b>    ||    </b> 
						<?php if(is_array($result)) echo '<a href="javascript:nextPage()"> Next Results </a>';?>
					</div>
		<?php
			}
		?>
	</div>

</body>

</html>



